// pages/money/money.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //tab框
        selected: 0,
        list: ['全部','已使用'],
        moneyList:[],
        moneyedList:[]
    },
    ///点击立即使用
    use:function(e){
        const id= e.currentTarget.dataset.id;
        wx.post('http://localhost:8000/wx/doSelectCoupon',{couponId: id,memberId:wx.getStorageSync('userInfo')._id}).then(res => {
            wx.showToast({
                title: '领取成功',
                icon:'success',
                duration: 2000
              })
          })
        this.onLoad()
    },
    //tab框
    selected: function (e) {
        let that= this
        let index = e.currentTarget.dataset.index
        console.log(index)
        if( index == 0){  
        that.setData({
            selected: 0
        })
        }else if( index == 1) {
        that.setData({
            selected: 1
        })
        }else{
        that.setData({
            selected: 2
        })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMoneyList();
        this.getMoneyedList();
    },
    getMoneyList(){
        wx.get('http://localhost:8000/wx/money/detail?memberId='+wx.getStorageSync('userInfo')._id).then(res => {
            res.forEach(a => {
                if (a.month){
                  let time = a.month
                  let date = new Date(time)
                  let times=date.getFullYear() + '-' + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1) )+ '-' + (date.getDate()<10?"0"+date.getDate():date.getDate() )
                  a.month = times
                }
              })
            this.setData({
                moneyList:res
            })
        })
      },
      getMoneyedList(){
        wx.get('http://localhost:8000/wx/getUsedCouponList?id='+wx.getStorageSync('userInfo')._id).then(res => {
            res.data.forEach(a => {
                if (a.month){
                  let time = a.month
                  let date = new Date(time)
                  let times=date.getFullYear() + '-' + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1) )+ '-' + (date.getDate()<10?"0"+date.getDate():date.getDate() )
                  a.month = times
                }
              })
            this.setData({
                moneyedList:res.data
            })
        })
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})